// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.Lua;

import java.util.ArrayList;
import se.krka.kahlua.integration.LuaCaller;
import se.krka.kahlua.luaj.compiler.LuaCompiler;
import se.krka.kahlua.vm.JavaFunction;
import se.krka.kahlua.vm.KahluaTable;
import se.krka.kahlua.vm.LuaCallFrame;
import se.krka.kahlua.vm.LuaClosure;
import se.krka.kahlua.vm.Platform;
import zombie.core.logger.ExceptionLogger;
import zombie.debug.DebugLog;
import zombie.debug.DebugOptions;

public final class Event {
    public static final int ADD = 0;
    public static final int NUM_FUNCTIONS = 1;
    private final Event.Add add;
    private final Event.Remove remove;
    public final ArrayList<LuaClosure> callbacks = new ArrayList();
    public String name;
    private int index = 0;

    public boolean trigger(KahluaTable var1, LuaCaller luaCaller, Object[] object) {
        if (this.callbacks.isEmpty()) {
            return false;
        } else if (DebugOptions.instance.Checks.SlowLuaEvents.getValue()) {
            for (int int0 = 0; int0 < this.callbacks.size(); int0++) {
                try {
                    LuaClosure luaClosure = (LuaClosure)this.callbacks.get(int0);
                    long _long = System.nanoTime();
                    luaCaller.protectedCallVoid(LuaManager.thread, luaClosure, object);
                    double _double = (double)(System.nanoTime() - _long) / 1000000.0;
                    if (_double > 250.0) {
                        DebugLog.Lua.warn("SLOW Lua event callback %s %s %dms", luaClosure.prototype.file, luaClosure, (int)_double);
                    }
                } catch (Exception exception0) {
                    ExceptionLogger.logException(exception0);
                }
            }

            return true;
        } else {
            for (int int1 = 0; int1 < this.callbacks.size(); int1++) {
                try {
                    luaCaller.protectedCallVoid(LuaManager.thread, this.callbacks.get(int1), object);
                } catch (Exception exception1) {
                    ExceptionLogger.logException(exception1);
                }
            }

            return true;
        }
    }

    public Event(String _name, int _index) {
        this.index = _index;
        this.name = _name;
        this.add = new Event.Add(this);
        this.remove = new Event.Remove(this);
    }

    public void register(Platform platform, KahluaTable kahluaTable1) {
        KahluaTable kahluaTable0 = platform.newTable();
        kahluaTable0.rawset("Add", this.add);
        kahluaTable0.rawset("Remove", this.remove);
        kahluaTable1.rawset(this.name, kahluaTable0);
    }

    public static final class Add implements JavaFunction {
        Event e;

        public Add(Event _e) {
            this.e = _e;
        }

        public int call(LuaCallFrame luaCallFrame, int var2) {
            if (LuaCompiler.rewriteEvents) {
                return 0;
            } else {
                Object object = luaCallFrame.get(0);
                if (this.e.name.contains("CreateUI")) {
                    boolean _boolean = false;
                }

                if (object instanceof LuaClosure luaClosure) {
                    this.e.callbacks.add(luaClosure);
                }

                return 0;
            }
        }
    }

    public static final class Remove implements JavaFunction {
        Event e;

        public Remove(Event _e) {
            this.e = _e;
        }

        public int call(LuaCallFrame luaCallFrame, int var2) {
            if (LuaCompiler.rewriteEvents) {
                return 0;
            } else {
                if (luaCallFrame.get(0) instanceof LuaClosure luaClosure) {
                    this.e.callbacks.remove(luaClosure);
                }

                return 0;
            }
        }
    }
}
